/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __SIMPLELIST_H__
#define __SIMPLELIST_H__

namespace liba
{

/** This class uses C-like methods for operating with the linked list. 
	It is intended to be used as the implementation of {@link liba::SimpleListNode}. */
class SimpleListNodeBase
{
protected:
	SimpleListNodeBase * next;
	SimpleListNodeBase * prev;

	void link( SimpleListNodeBase * start )
	{
		SimpleListNodeBase * nn = start->next;
		next = nn;
		if( nn )
			nn->prev = this;
		prev = start;
		start->next = this;
	}
public:
	SimpleListNodeBase()
		:	prev( 0 ),
			next( 0 )
	{}

	SimpleListNodeBase(const SimpleListNodeBase &other)
		:	prev( 0 ),
			next( 0 )
	{}

	void unlink()
	{
		SimpleListNodeBase * pp = prev;
		SimpleListNodeBase * nn = next;

		pp->next = nn;
		if( nn )
			nn->prev = pp;
		prev = next = 0;
	}
};

/** This class adds type-safe interface to {@link liba::SimpleListNodeBase}. */
template<typename T>
class SimpleListNode : public SimpleListNodeBase
{
public:
	void link( SimpleListNode<T> * start )
	{
		SimpleListNodeBase::link( start );
	}
	T * get_next()const
	{
		return static_cast<T*>( next );
	}
};

} // namespace liba
using namespace liba;


#endif //__SIMPLELIST_H__